﻿using System;
using System.Collections.Generic;
using System.Text;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;

namespace Hotel.Service
{

    /// <summary>
    /// 客房涉及相关业务逻辑处理服务
    /// </summary>
    public class RoomService
    {

        private static RoomService singleRoomService = null;


        private RoomService()
        { 
        
        
        }
        public static RoomService Instance
        {
            get
            {
                if (singleRoomService == null)
                {

                    singleRoomService = new RoomService();


                }

                return singleRoomService;

            }
        
        }



        /// <summary>
        /// 根据房间号获取消费列表(包括已结账和未结账)
        /// </summary>
        /// <param name="roomNum"></param>
        /// <returns></returns>
        public List<ConsumerListInfo> GetConsumerList(string roomNum)
        {

            List<ConsumerListInfo> mConsumerList = new List<ConsumerListInfo>();
           //先获取当前订单，根据订单获取账号，根据账号获取消费列表
            OrderInfo mOrderInfo = BLLFactory<Order>.Instance.GetCurrentOrder(roomNum);
            return this.GetConsumerList(mOrderInfo);
        
        }


        /// <summary>
        /// 根据订单信息回去消费列表
        /// </summary>
        /// <param name="mOrderInfo"></param>
        /// <returns></returns>
        public List<ConsumerListInfo> GetConsumerList(OrderInfo mOrderInfo)
        {

            List<ConsumerListInfo> mConsumerList = new List<ConsumerListInfo>();
            //先获取当前订单，根据订单获取账号，根据账号获取消费列表
            if (mOrderInfo != null)
            {



                if (mOrderInfo.CheckType == 0)//散客
                {

                    mConsumerList = BLLFactory<Room>.Instance.GetGoodsConsumption(mOrderInfo.RoomNo);



                }
                else //团体
                {

                    OrderInfo teamVirtualOrder = null;
                    teamVirtualOrder = BLLFactory<Order>.Instance.FindSingle(" MasterRoomNo='" + mOrderInfo.MasterRoomNo + "'  and   IsVirtualAccount=1 ");
                    if (teamVirtualOrder != null)
                    {

                        mConsumerList = BLLFactory<ConsumerList>.Instance.Find("   Account='" + teamVirtualOrder.BillNumber + "'  ");


                    }


                }
            }


            return mConsumerList;
        
        
        }

        /// <summary>
        /// 获取所有房间的信息
        /// todo:优化改造
        /// </summary>
        /// <returns></returns>
        public List<RoomInfo> GetRoomStatusViewList(string condition)
        {
            List<RoomInfo> list = BLLFactory < WHC.Hotel.BLL.Room>.Instance.Find(condition);
            BookInfo mBookInfo = null;

            foreach (RoomInfo item in list)
            {

                //获取最近预定信息（指定房号）    
                mBookInfo = BLLFactory<WHC.Hotel.BLL.Book>.Instance.FindSingle("   RoomNo='" + item.RoomNo + "' and (IsCancel is null or IsCancel=0) and PreArrivalTime=( select min(PreArrivalTime) from  KF_Book  where   RoomNo='" + item.RoomNo + "' and (IsCancel is null or IsCancel=0)) ");
                if (mBookInfo != null)
                {

                    item.BookBeginTime = mBookInfo.PreArrivalTime;
                    item.BookEndTime = mBookInfo.PreOffTime;
                    item.IsExistBook = true;

                }

                if (item.Status == RoomStatus.住净房 || item.Status == RoomStatus.住脏房)
                {

                    item.OrderInfo = BLLFactory<WHC.Hotel.BLL.Order>.Instance.GetCurrentOrder(item.RoomNo);
                    if (item.OrderInfo.IsPartTime)
                    {

                        item.IsClockRoom = true;


                    }
                    //获取消费总额
                    List<ConsumerListInfo> mConsumerList = this.GetConsumerList(item.OrderInfo);

                    decimal mConsumedSum = 0.0M;
                    foreach (ConsumerListInfo mConsumerListInfo in mConsumerList)
                    {
                        if (mConsumerListInfo.IsCheckout == 0)
                        {
                            mConsumedSum += mConsumerListInfo.Amount;

                        }

                    }
                    item.ConsumedSum = mConsumedSum;

                    if (item.OrderInfo.PreOffDate.Date == DateTime.Now.Date)
                    {

                         item.IsTodayLeave = true;
                    
                    }




                }

                RoomTypeInfo mRoomTypeInfo = BLLFactory<WHC.Hotel.BLL.RoomType>.Instance.FindSingle("    Name='" + item.RoomType + "'");
                if (mRoomTypeInfo != null)
                {


                    item.RoomTypeCode = mRoomTypeInfo.Note;

                }




            }



            return list;
        }




    }
}
